package com.fqgj.xjd.user.dao.impl;

import com.fqgj.common.base.AbstractBaseMapper;
import com.fqgj.common.utils.StringUtils;
import com.fqgj.xjd.user.dao.UserAccessDao;
import com.fqgj.xjd.user.entity.UserAccessEntity;
import com.fqgj.xjd.user.entity.business.UserAccessInfoEntity;
import org.springframework.stereotype.Repository;

import java.util.*;

/**
 * User: zhangxingyun
 * Date: 2017-07-22
 * Time: 21:48:31.105
 */

@Repository
public class UserAccessDaoImpl extends AbstractBaseMapper<UserAccessEntity> implements UserAccessDao {

    @Override
    public UserAccessEntity selectUserAccessByUserCode(String userCode) {
        Map params = new HashMap();
        params.put("userCode", userCode);
        return getSqlSession().selectOne(getStatement("selectUserAccessByUserCode"), params);
    }

    @Override
    public UserAccessEntity selectUserAccessByToken(String token) {
        return getSqlSession().selectOne(getStatement("selectUserAccessByToken"), token);
    }

    @Override
    public UserAccessEntity selectUserByMobileAndAppCode(String mobile, String code) {
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("appCode", String.valueOf(code));
        return getSqlSession().selectOne(getStatement("selectUserByMobileAndAppCode"), map);
    }

    @Override
    public UserAccessEntity selectUserByMobile(String mobile) {
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        return getSqlSession().selectOne(getStatement("selectUserByMobile"), map);
    }

    @Override
    public UserAccessEntity selectUserByMD5MobileAndAppCode(String md5Mobile, String code) {
        Map<String, Object> map = new HashMap<>();
        map.put("md5Mobile", md5Mobile);
        map.put("appCode", String.valueOf(code));
        return getSqlSession().selectOne(getStatement("selectUserByMD5MobileAndAppCode"), map);
    }

    @Override
    public UserAccessEntity selectUserByMobileAndAppCodeAndBiz(String mobile, String code, Integer biz) {
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        map.put("appCode", String.valueOf(code));
        map.put("biz", biz);
        return getSqlSession().selectOne(getStatement("selectUserByMobileAndAppCodeAndBiz"), map);
    }

    @Override
    public List<UserAccessEntity> selectUserListByMobileListAndAppCode(List<String> mobileList, List<String> categories) {
        Map<String, Object> map = new HashMap<>();
        map.put("mobileList", mobileList);
        map.put("categories", categories);
        return getSqlSession().selectList(getStatement("selectUserListByMobileListAndAppCode"), map);
    }

    @Override
    public List<UserAccessEntity> selectByCustomerCode(String customerCode) {
        return getSqlSession().selectList(getStatement("selectByCustomerCode"), customerCode);
    }

    @Override
    public List<UserAccessEntity> selectListByCustomerCode(List<String> customerCodes) {
        return getSqlSession().selectList(getStatement("selectListByCustomerCode"), customerCodes);
    }

    @Override
    public List<UserAccessEntity> selectListByUserCodes(List<String> userCodeList) {
        return getSqlSession().selectList(getStatement("selectListByUserCodes"), userCodeList);
    }

    @Override
    public int updateCustomerCodeByUserCode(String userCode, String customerCode) {
        Map<String, Object> map = new HashMap<>();
        map.put("userCode", userCode);
        map.put("customerCode", customerCode);
        map.put("gmtModified", new Date());
        return getSqlSession().update(getStatement("updateCustomerCodeByUserCode"), map);
    }

    @Override
    public List<UserAccessEntity> selectListByUserIds(List<Long> userIds) {
        return getSqlSession().selectList(getStatement("selectListByUserIds"), userIds);
    }

    @Override
    public Integer countByCondition(Integer appCode, String mobile, String identityNo, String name) {
        Map<String, Object> map = new HashMap<>();
        map.put("appCode", String.valueOf(appCode));
        map.put("mobile", mobile);
        map.put("identityNo", identityNo);
        map.put("name", name);
        return getSqlSession().selectOne(getStatement("countByCondition"), map);
    }

    @Override
    public List<UserAccessEntity> selectListByCondition(Integer appCode, String mobile, String identityNo, String name, Integer startIndex, Integer pageSize, List<String> categories) {
        Map<String, Object> map = new HashMap<>();
        map.put("appCode", String.valueOf(appCode));
        map.put("mobile", mobile);
        map.put("identityNo", identityNo);
        map.put("name", name);
        map.put("startIndex", startIndex);
        map.put("pageSize", pageSize);
        map.put("categories", categories);

        return getSqlSession().selectList(getStatement("selectListByCondition"), map);
    }

    @Override
    public List<UserAccessInfoEntity> selectUserAccessListByMobile(String mobile) {
        if (StringUtils.isEmpty(mobile)) {
            return new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("mobile", mobile);
        return getSqlSession().selectList(getStatement("selectUserAccessListByMobile"), map);
    }

    @Override
    public List<UserAccessInfoEntity> selectUserAccessListByMD5Mobile(String md5Mobile) {
        if (StringUtils.isEmpty(md5Mobile)) {
            return new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("md5Mobile", md5Mobile);
        return getSqlSession().selectList(getStatement("selectUserAccessListByMobile"), map);
    }

    @Override
    public List<UserAccessInfoEntity> selectUserAccessListByMD5MobileIdNo(String md5MobileIdNo) {
        if (StringUtils.isEmpty(md5MobileIdNo)) {
            return new ArrayList<>();
        }
        Map<String, Object> map = new HashMap<>();
        map.put("md5MobileIdentityNo", md5MobileIdNo);
        return getSqlSession().selectList(getStatement("selectUserAccessListByMobile"), map);
    }
}